package com.qf.bank.redis.utils;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author 惠佳旭
 * @date 2019/12/4
 */
@Component
public class RedisUtils {

  @Autowired private RedisTemplate redisTemplate;
  @Autowired private RabbitTemplate rabbitTemplate;

  public void addString(String key, Object value) {
    List<Object> list = new LinkedList<>();
    list.add(key);
    list.add(value);
    rabbitTemplate.convertAndSend("topic.exchange", "topic.redis.string", list);
  }

  public Object getString(String key) {
    return redisTemplate.opsForValue().get(key);
  }

  @RabbitListener(queues = "topic.redis.string")
  private void redisString(List list) {
    redisTemplate.opsForValue().set(list.get(0), list.get(1));
  }

  public void addHash(String key, Map<String, Object> value) {
    List<Object> list = new LinkedList<>();
    list.add(key);
    list.add(value);
    rabbitTemplate.convertAndSend("topic.exchange", "topic.redis.hash", list);
  }

  //  public void addHashByField(String key, String field, Object value) {
  //    List<Object> list = new LinkedList<>();
  //    list.add(key);
  //    list.add(field);
  //    list.add(value);
  //  }

  public List getHash(String key) {
    return redisTemplate.opsForHash().values(key);
  }

  public Object getHashByField(String key, String field) {
    return redisTemplate.opsForHash().get(key, field);
  }

  @RabbitListener(queues = "topic.redis.hash")
  private void redisHash(List list) {
    redisTemplate.opsForHash().putAll(list.get(0), (Map) list.get(1));
  }

  public void addList(String key, List value) {
    List<Object> list = new LinkedList<>();
    list.add(key);
    list.add(value);
    rabbitTemplate.convertAndSend("topic.exchange", "topic.redis.list", list);
  }

  public List getList(String key) {
    return redisTemplate.opsForList().range(key, 0L, -1L);
  }

  public Object getListByRange(String key, long begin, long end) {
    return redisTemplate.opsForList().range(key, begin, end);
  }

  @RabbitListener(queues = "topic.redis.list")
  private void redisList(List list) {
    List values = (List) list.get(1);
    values.forEach(value -> redisTemplate.opsForList().leftPush(list.get(0), value));
  }

  public void addSet(String key, Set value) {
    List<Object> list = new LinkedList<>();
    list.add(key);
    list.add(value);
    rabbitTemplate.convertAndSend("topic.exchange", "topic.redis.set", list);
  }

  public Set getSet(String key) {
    return redisTemplate.opsForSet().members(key);
  }

  @RabbitListener(queues = "topic.redis.set")
  private void redisSet(List list) {
    Set values = (Set) list.get(1);
    values.forEach(value -> redisTemplate.opsForSet().add(list.get(0), value));
  }

  /**
   * 添加一个有序集合
   *
   * @param key
   * @param value
   */
  public void addZset(String key, Map<Double, Object> value) {
    List<Object> list = new LinkedList<>();
    list.add(key);
    list.add(value);
    rabbitTemplate.convertAndSend("topic.exchange", "topic.redis.zset", list);
  }

  public Set getZset(String key) {
    return redisTemplate.opsForZSet().range(key, 0L, -1L);
  }

  public Set getZsetByRange(String key, long begin, long end) {
    return redisTemplate.opsForZSet().range(key, begin, end);
  }

  @RabbitListener(queues = "topic.redis.zset")
  private void redisZset(List list) {
    Map<Double, Object> map = (Map<Double, Object>) list.get(1);
    map.forEach((score, value) -> redisTemplate.opsForZSet().add(list.get(0), value, score));
  }
}
